from django.utils.deprecation import MiddlewareMixin

from django.shortcuts import HttpResponse,redirect,render


class M1_login_detection(MiddlewareMixin):
    """"""
    """登录检测中间件"""

    def process_request(self, request):
        """"""
        """检测是否登录"""
        if request.path_info in ['/login/', '/image/code/', '/register/', '/admin/',]:
            return
        if '/user/active/' in request.path_info:
            return
        info = request.session.get('info')
        if not info:
            return redirect('/login/')
        if info["is_active"] != 1:
            return HttpResponse("账户未激活,请先激活")
        if info["level"] == 2:
            return HttpResponse("账户异常,已冻结")
        if request.path_info in ["/management/"]:
            level = request.session.get('info')["level"]
            if level != 3:
                return redirect('/login/')


